package Test1;

public class Staff {
    private String name;
    private int age;

    public Staff(String name, int age){
        setName(name);
        setAge(age);
    }

    public Staff() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        //判断姓名，若不满足条件则抛出异常
        if(name == "" || name == null){
            try {
                throw new StaffException("名字设置不合理！");
            } catch (StaffException e) {
                e.printStackTrace();
            }
        }else{
            this.name = name;
        }
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "Staff{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void setAge(int age)  {
        //判断年龄，若不满足条件则抛出异常
        if(age > 150 || age < 18){
            try {
                throw new StaffException("年龄设置不合理！");
            } catch (StaffException e) {
                e.printStackTrace();
            }
        }else{
            this.age = age;
        }
    }
}
